<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class BtApiController extends Controller
{
    private $BT_KEY = "";  //接口密钥
    private $BT_PANEL = "";	   //面板地址

    //如果希望多台面板，可以在实例化对象时，将面板地址与密钥传入
    public function __construct(){
        if($bt_panel) $this->BT_PANEL = config('constant.bt_panel');
        if($bt_key) $this->BT_KEY = config('constant.bt_key');
    }

    //公共调用函数(不用一个一个写了)
    public function getData($params=[],$url='/data?action=getData'){
        //拼接URL地址
        $url = $this->BT_PANEL.$url;
        //准备POST数据
        $p_data = $this->GetKeyData();		//取签名
        $p_data = array_merge($p_data,$params);
        return $this->rtdata($url,$p_data);
    }

    //搜索数据库
    public function searchDB($dbname){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData';

        //准备POST数据
        $p_data = $this->GetKeyData();		//取签名
        $p_data['tojs'] = 'database.get_list';
        $p_data['table'] = 'databases';
        $p_data['search'] = $dbname;
        return $this->rtdata($url,$p_data);
    }

    //备份数据库
    public function toBackUp($id){
        //拼接URL地址
        $url = $this->BT_PANEL.'/database?action=ToBackup';
        //准备POST数据
        $p_data = $this->GetKeyData();		//取签名
        $p_data['id'] = $id;
        return $this->rtdata($url,$p_data);
    }

    //获取最近一次备份数据库
    public function getBackUp($id){
        //拼接URL地址
        $url = $this->BT_PANEL.'/data?action=getData';
        //准备POST数据
        $p_data = $this->GetKeyData();		//取签名
        $p_data['table'] = 'backup';
        $p_data['search'] = $id;
        $p_data['type'] = 1;
        $p_data['order'] = 'id desc';
        $p_data['tojs'] = 'database.database_detail';
        return $this->rtdata($url,$p_data);
    }

    //删除指定ID的备份
    public function delBackUp($id){
        //拼接URL地址
        $url = $this->BT_PANEL.'/database?action=DelBackup';
        //准备POST数据
        $p_data = $this->GetKeyData();		//取签名
        $p_data['id'] = $id;
        return $this->rtdata($url,$p_data);
    }

    /**
     * 统一返回数据格式
     */
    private function rtdata($url,$p_data){
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);
        //解析JSON数据
        $data = json_decode($result,true);
        return $data;
    }

    /**
     * 构造带有签名的关联数组
     */
    private function GetKeyData(){
        $now_time = time();
        $p_data = array(
            'request_token'	=>	md5($now_time.''.md5($this->BT_KEY)),
            'request_time'	=>	$now_time
        );
        return $p_data;
    }


    /**
     * 发起POST请求
     * @param String $url 目标网填，带http://
     * @param Array|String $data 欲提交的数据
     * @return string
     */
    private function HttpPostCookie($url, $data,$timeout = 60)
    {
        //定义cookie保存位置
        $disk = Storage::disk('database');
        $filename = md5($this->BT_PANEL).'.cookie';
        $cookie_file = $disk->getAdapter()->getPathPrefix().'/'.$filename;
        if(!$disk->exists($filename)){
            $disk->put($filename,'');
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
